<?php
$data[title].='Allianz Statistik';
$data[head].=''; 

switch($user_level){
	case 2:	$add=' AND spieler.alli='.$user_alli;
	break;
	case 1: $add=' AND spieler.id='.$user_id;
	break;
	default: $add='';
	break;
};

if(isset($_GET['select_allianz']) && $_GET['select_allianz'] == "true"){
	$_SESSION['SELECT_ALLIANZ'] = true;
}elseif(isset($_GET['select_allianz']) && $_GET['select_allianz'] == "false"){
	$_SESSION['SELECT_ALLIANZ'] = false;
};

if(isset($_SESSION['SELECT_ALLIANZ']) && $_SESSION['SELECT_ALLIANZ']){
	$add.=' AND spieler.alli='.$user_alli;
};


$sql='
SELECT 
	count(spieler.id) AS player,
	alli.id, 
	alli.name, 
	alli.tag, 
 	SUM((lj*50+sj*150+xer*400+ss*1000+bomb*1000+zer*2000+tod*200000)*(Waffentechnik/10+1)) AS att,
	SUM(Spionagetechnik+Computertechnik+Waffentechnik+Schildtechnik+Raumschiffpanzerung+Energietechnik+Verbrennungstriebwerk+Impulstriebwerk+Lasertechnik+Ionentechnik+Plasmatechnik+gravi+hyperantrieb+forschungsnetz+hyperraumtechnik) AS forsch
FROM 
	fleet,
	forschung,
	alli,
	spieler
WHERE 
	spieler.id=fleet.spieler_id
AND
	spieler.id=forschung.spieler_id
AND
	spieler.alli=alli.id
AND
	alli.show_rangliste=1
GROUP BY 
	alli.id';
$query=@mysql_query($sql);
$rows=@mysql_fetch_assoc($query);

$sql='
SELECT 
	alli.id, 
	FLOOR((SUM(planfelder/1000*planfelder/1000)+(SUM(terraformer*5)))/(COUNT(planfelder))) AS size,
	SUM(raketenwerfer*2) + SUM(leichterlaser*2) + SUM(schwererlaser*8) + SUM(ionengeschutz*8) + SUM(gauskanone*37) + SUM(klschild*20) + SUM(plasmawerfer*130) + SUM(grschild*100) + SUM(abfangrakete*10) + SUM(interplanetarrakete*25) AS defe,
	FLOOR(SUM(30*metallmine* POW(1.1, metallmine))) AS met,
	FLOOR(SUM(20*kristallmine* POW(1.1, kristallmine))) AS kris,
	FLOOR(SUM(10*deuteriumsynthetisierer*POW(1.1, deuteriumsynthetisierer)*(temp*(-0.002) + 1.28))) AS deut,
	COUNT(planeten.id) AS planis,
	SUM(allianzdepot+metallmine+kristallmine+deuteriumsynthetisierer+solarkraftwerk+fusionskraftwerk+roboterfabrik+nanitenfabrik+terraformer+raumschiffwerft+metallspeicher+kristallspeicher+deutriumspeicher+forschungslabor+raketensilo)/(SUM(planfelder/1000*planfelder/1000)+SUM(terraformer*5))*100 AS bau
FROM 
	alli,
	spieler
LEFT JOIN planeten ON spieler.id=planeten.spieler
WHERE 
	alli.id = spieler.alli
AND
	alli.show_rangliste=1
GROUP BY 
	alli.id';
$query_p=@mysql_query($sql);
$rows_p=@mysql_fetch_assoc($query_p);

do{
	$x=$rows['id'];
	$allianz[$x]['name']		= $rows['name'];
	$allianz[$x]['tag']			= $rows['tag'];
	$allianz[$x]['id']			= $rows['id'];
	$allianz[$x]['def_d']		= $rows['player'];
	$allianz[$x]['forschung']	= number_format($rows['forsch'], 0, ',', '.');
	if($rows['player']>0){
		$allianz[$x]['forschung_d']	= number_format(($rows['forsch']/$rows['player']), 0, ',', '.');
	}else{
		$allianz[$x]['forschung_d']	= number_format(($rows['forsch']), 0, ',', '.');
	}
	$allianz[$x]['aw']			= number_format($rows['att'], 0, ',', '.');
	if($rows['player']>0){
		$allianz[$x]['aw_d']		= number_format(($rows['att']/$rows['player']), 0, ',', '.');
	}else{
		$allianz[$x]['aw_d']		= number_format(($rows['att']), 0, ',', '.');
	}
	$allianz[$x]['spieler']		= number_format($rows['player'], 0, ',', '.');
}while($rows=@mysql_fetch_assoc($query));

do{
	$x=$rows_p['id'];
	$allianz[$x]['psize']		= number_format($rows_p['size'], 0, ',', '.');
	$allianz[$x]['def']			= number_format($rows_p['defe'], 0, ',', '.');
	if($allianz[$x]['def_d']>0){
		$allianz[$x]['def_d']		= number_format(($rows_p['defe']/$allianz[$x]['def_d']), 0, ',', '.');
	}else{
		$allianz[$x]['def_d']		= number_format(($rows_p['defe']), 0, ',', '.');
	};
	$allianz[$x]['bau']			= number_format($rows_p['bau'], 0, ',', '.');
	$allianz[$x]['planeten']	= number_format($rows_p['planis'], 0, ',', '.');
	$allianz[$x]['met']			= number_format(($rows_p['met']*24/1000), 0, ',', '.');
	$allianz[$x]['met_w']		= number_format(($rows_p['met']*24*7/1000), 0, ',', '.');
	$allianz[$x]['kris']		= number_format(($rows_p['kris']*24/1000), 0, ',', '.');
	$allianz[$x]['kris_w']		= number_format(($rows_p['kris']*24*7/1000), 0, ',', '.');
	$allianz[$x]['deut']		= number_format(($rows_p['deut']*24/1000), 0, ',', '.');
	$allianz[$x]['deut_w']		= number_format(($rows_p['deut']*24*7/1000), 0, ',', '.');
}while($rows_p=@mysql_fetch_assoc($query_p));

//echo '<pre>';
//print_r($allianz);
//echo '</pre>';

$sort_1		= '';
$sort_2		= '';
$sort_3		= '';
$sort_4		= '';
$sort_5		= '';
$sort_6		= '';
$sort_7		= '';
$sort_8		= '';
$sort_9		= '';
$sort_10	= '';
$sort_11	= '';
$sort_12	= '';
$sort_13	= '';
if(isset($_GET['sort'])){
	switch ($_GET['sort']){
		case 1:
			if(isset($_GET['way']) && $_GET['way']==2){ 
				$allianz = multisort($allianz, "'name'", false, 4); 
			}else {
				$allianz = multisort($allianz, "'name'", true, 4);
				$sort_1 = '&way=2';
			};
		break;
		case 2:
			if(isset($_GET['way']) && $_GET['way']==2){ 
				$allianz = multisort($allianz, "'planeten'", false, 4);
			}else {
				$allianz = multisort($allianz, "'planeten'", true, 4);
				$sort_2 = '&way=2';
			};
		break;
		case 3:
			if(isset($_GET['way']) && $_GET['way']==2){ 
				$allianz = multisort($allianz, "'psize'", true, 4);
			}else {
				$allianz = multisort($allianz, "'psize'", false, 4);
				$sort_3 = '&way=2';
			};
		break;
		case 4:
			if(isset($_GET['way']) && $_GET['way']==2){ 
				$allianz = multisort($allianz, "'spieler'", false, 4);
			}else {
				$allianz = multisort($allianz, "'spieler'", true, 4);
				$sort_4 = '&way=2';
			};
		break;
		case 5:
			if(isset($_GET['way']) && $_GET['way']==2){ 
				$allianz = multisort($allianz, "'def'", true, 4);
			}else {
				$allianz = multisort($allianz, "'def'", false, 4);
				$sort_5 = '&way=2';
			};
		break;
		case 6:
			if(isset($_GET['way']) && $_GET['way']==2){ 
				$allianz = multisort($allianz, "'aw'", true, 4);
			}else {
				$allianz = multisort($allianz, "'aw'", false, 4);
				$sort_6 = '&way=2';
			};
		break;
		case 7:
			if(isset($_GET['way']) && $_GET['way']==2){ 
				$allianz = multisort($allianz, "'forschung'", true, 4);
			}else {
				$allianz = multisort($allianz, "'forschung'", false, 4);
				$sort_7 = '&way=2';
			};
		break;
		case 8:
			if(isset($_GET['way']) && $_GET['way']==2){ 
				$allianz = multisort($allianz, "'met'", false);
			}else {
				$allianz = multisort($allianz, "'met'");
				$sort_8 = '&way=2';
			};
		break;
		case 9:
			if(isset($_GET['way']) && $_GET['way']==2){ 
				$allianz = multisort($allianz, "'kris'", false);
			}else {
				$allianz = multisort($allianz, "'kris'");
				$sort_9 = '&way=2';
			};
		break;
		case 10:
			if(isset($_GET['way']) && $_GET['way']==2){ 
				$allianz = multisort($allianz, "'deut'", false);
			}else {
				$allianz = multisort($allianz, "'deut'");
				$sort_10 = '&way=2';
			};
		break;
		case 11:
			if(isset($_GET['way']) && $_GET['way']==2){ 
				$allianz = multisort($allianz, "'def_d'", false, 4);
			}else {
				$allianz = multisort($allianz, "'def_d'", true, 4);
				$sort_11 = '&way=2';
			};
		break;
		case 12:
			if(isset($_GET['way']) && $_GET['way']==2){ 
				$allianz = multisort($allianz, "'aw_d'", true, 4);
			}else {
				$allianz = multisort($allianz, "'aw_d'", false, 4);
				$sort_12 = '&way=2';
			};
		break;
		case 13:
			if(isset($_GET['way']) && $_GET['way']==2){ 
			$allianz = multisort($allianz, "'forschung_d'", false);
			}else {
				$allianz = multisort($allianz, "'forschung_d'");
				$sort_13 = '&way=2';
			};
		break;
		default:
			if(isset($_GET['way']) && $_GET['way']==2){ 
				$allianz = multisort($allianz, "'name'", false);
			}else {
				$allianz = multisort($allianz, "'name'");
				$sort_1 = '&way=2';
			};
		break;
	};
};

$data[body].='<h2>Allianz Statistik</h2>
<table border="0" cellpadding="00" cellspacing="0">
  <tr>
    <th align="center">&nbsp;</th>
    <th align="center"><a href="'.$dir.'?screen=statts_a&sort=1'.$sort_1.'">Name</a></th>
    <th align="right"><a href="'.$dir.'?screen=statts_a&sort=2'.$sort_2.'">Planeten</a></th>
    <th align="right"><a href="'.$dir.'?screen=statts_a&sort=3'.$sort_3.'">Felder</a></th>
    <th align="right"><a href="'.$dir.'?screen=statts_a&sort=4'.$sort_4.'">Spieler</a></th>
    <th align="right"><a href="'.$dir.'?screen=statts_a&sort=5'.$sort_5.'">Def (all)</a></th>
    <th align="right"><a href="'.$dir.'?screen=statts_a&sort=11'.$sort_11.'">Def</a></th>
    <th align="right"><a href="'.$dir.'?screen=statts_a&sort=6'.$sort_6.'">AW (all)</a></th>
    <th align="right"><a href="'.$dir.'?screen=statts_a&sort=12'.$sort_12.'">AW</a></th>
    <th align="right"><a href="'.$dir.'?screen=statts_a&sort=7'.$sort_7.'">Forschung (all)</a></th>
    <th align="right"><a href="'.$dir.'?screen=statts_a&sort=13'.$sort_13.'">Forschung</a></th>
    <th align="right"><a href="'.$dir.'?screen=statts_a&sort=8'.$sort_8.'">Met</a></th>
    <th align="right"><a href="'.$dir.'?screen=statts_a&sort=8'.$sort_8.'">Met (w)</a></th>
    <th align="right"><a href="'.$dir.'?screen=statts_a&sort=9'.$sort_9.'">Kris</a></th>
    <th align="right"><a href="'.$dir.'?screen=statts_a&sort=9'.$sort_9.'">Kris (w)</a></th>
    <th align="right"><a href="'.$dir.'?screen=statts_a&sort=10'.$sort_10.'">Deut</a></th>
    <th align="right"><a href="'.$dir.'?screen=statts_a&sort=10'.$sort_10.'">Deut (w)</a></th>
  </tr>';
$count=0; 
foreach($allianz as $part) { 
	$count++;
	if(isset($_SESSION['USER_ALLIANZ']) && $_SESSION['USER_ALLIANZ'] == $part['tag']){
		$background='background="'.$dir.'design/images/galaxy_04.'.$data[img_end].'" ';
	}elseif($count % 2 == 0){
		$background='background="'.$dir.'design/images/list_second.'.$data[img_end].'" ';
	}else{
		$background='';
	};
	$data[body].='<tr>
	  <td '.$background.' height="35" align="center" height="20px">'.$count.'</td>
      <td '.$background.' align="center"><a href="'.$dir.'?screen=allianzen#'.$part['id'].'">'.$part['tag'].'</a></td>
      <td '.$background.' align="right">'.$part['planeten'].'</td>
      <td '.$background.' align="right">'.$part['psize'].'</td>
      <td '.$background.' align="right">'.$part['spieler'].'</td>
      <td '.$background.' align="right">'.$part['def'].'</td>
      <td '.$background.' align="right">'.$part['def_d'].'</td>
      <td '.$background.' align="right">'.$part['aw'].'</td>
      <td '.$background.' align="right">'.$part['aw_d'].'</td>
      <td '.$background.' align="right">'.$part['forschung'].'</td>
      <td '.$background.' align="right">'.$part['forschung_d'].'</td>
      <td '.$background.' align="right">'.$part['met'].'k </td>
      <td '.$background.' align="right">'.$part['met_w'].'k </td>
      <td '.$background.' align="right">'.$part['kris'].'k </td>
      <td '.$background.' align="right">'.$part['kris_w'].'k </td>
      <td '.$background.' align="right">'.$part['deut'].'k </td>
      <td '.$background.' align="right">'.$part['deut_w'].'k </td>
    </tr>';
} ;
$data[body].='</table>';
?>
